package cn.stylefeng.guns.modular.order.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.core.util.DateUtil;
import cn.stylefeng.guns.modular.basic.model.params.BasicGoodsParam;
import cn.stylefeng.guns.modular.basic.service.BasicGoodsService;
import cn.stylefeng.guns.modular.order.entity.OrderPutDetailed;
import cn.stylefeng.guns.modular.order.model.params.OrderPutDetailedParam;
import cn.stylefeng.guns.modular.order.service.OrderPutDetailedService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.core.util.ToolUtil;

/**
 * 入库订单明细表控制器
 */
@Controller
@RequestMapping("/orderPutDetailed")
public class OrderPutDetailedController extends BaseController {

    private String PREFIX = "/order/orderPutDetailed";

    @Autowired
    private OrderPutDetailedService orderPutDetailedService;

    @Autowired
    private UserService userService;

    @Autowired
    private BasicGoodsService basicGoodsService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/orderPutDetailed.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(@RequestParam("putDetailedNumber") String putDetailedNumber,
    		@RequestParam("putCustomerId") Long putCustomerId,Model model) {
    	model.addAttribute("putDetailedNumber", putDetailedNumber);
    	model.addAttribute("putCustomerId", putCustomerId);
        return PREFIX + "/orderPutDetailed_add.html";
    }

    /**
     * 编辑页面
     */
    @RequestMapping("/edit")
    public String edit(@RequestParam("putDetailedNumber") String putDetailedNumber,
    		@RequestParam("putCustomerId") Long putCustomerId,Model model) {
    	model.addAttribute("putDetailedNumber", putDetailedNumber);
    	model.addAttribute("putCustomerId", putCustomerId);
        return PREFIX + "/orderPutDetailed_edit.html";
    }

    /**
     * 选择商品页面
     */
    @RequestMapping("/toChoiceGoods")
    public String toChoiceGoods() {
        return PREFIX + "/toChoiceGoods.html";
    }

    /**
     * 选择商品页面
     */
    @RequestMapping("/toChoiceGoods2")
    public String toChoiceGoods2() {
        return PREFIX + "/toChoiceGoods2.html";
    }

    /**
     * 查询商品列表
     */
    @ResponseBody
    @RequestMapping("/ChoiceGoods")
    public LayuiPageInfo ChoiceGoods(BasicGoodsParam basicGoodsParam,String condition) {
    	User user = userService.getcurrentUser();
        return this.basicGoodsService.findPageBySpec(basicGoodsParam,condition,user.getDeptId(),user.getOperateId());
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(OrderPutDetailedParam orderPutDetailedParam) {
    	if(ToolUtil.isEmpty(orderPutDetailedParam.getDetailedDateofmanufacture())) {
    		orderPutDetailedParam.setDetailedDateofmanufacture(DateUtil.getDate());
    	}
    	if(ToolUtil.isEmpty(orderPutDetailedParam.getDetailedBatchnumber())) {
    		orderPutDetailedParam.setDetailedBatchnumber(orderPutDetailedParam.getDetailedDateofmanufacture().replaceAll("-", ""));
    	}
    	if (ToolUtil.isEmpty(orderPutDetailedParam.getRecommend())) {
    		orderPutDetailedParam.setRecommend(DateUtil.getDate());
		}
    	String goodsname = orderPutDetailedParam.getDetailedGoodsname();
    	//解决参数中的 英文括号
    	if (goodsname!=null&&goodsname!="") {
    		goodsname = goodsname.replace("& #40;", "(");
    		goodsname = goodsname.replace("& #41;", ")");
    		orderPutDetailedParam.setDetailedGoodsname(goodsname);
    	}
    	String goodsNo = orderPutDetailedParam.getGoodsNo();
    	//解决参数中的 英文括号
    	if (goodsNo!=null&&goodsNo!="") {
    		goodsNo = goodsNo.replace("& #40;", "(");
    		goodsNo = goodsNo.replace("& #41;", ")");
    		orderPutDetailedParam.setGoodsNo(goodsNo);
    	}
        this.orderPutDetailedService.add(orderPutDetailedParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(OrderPutDetailedParam orderPutDetailedParam) {
    	if(ToolUtil.isEmpty(orderPutDetailedParam.getDetailedDateofmanufacture())) {
    		orderPutDetailedParam.setDetailedDateofmanufacture(DateUtil.getDate());
    	}
    	if(ToolUtil.isEmpty(orderPutDetailedParam.getDetailedBatchnumber())) {
    		orderPutDetailedParam.setDetailedBatchnumber(orderPutDetailedParam.getDetailedDateofmanufacture().replaceAll("-", ""));
    	}
    	if (ToolUtil.isEmpty(orderPutDetailedParam.getRecommend())) {
    		orderPutDetailedParam.setRecommend(DateUtil.getDate());
		}
    	String goodsname = orderPutDetailedParam.getDetailedGoodsname();
    	//解决参数中的 英文括号
    	if (goodsname!=null&&goodsname!="") {
    		goodsname = goodsname.replace("& #40;", "(");
    		goodsname = goodsname.replace("& #41;", ")");
    		orderPutDetailedParam.setDetailedGoodsname(goodsname);
    	}
    	String goodsNo = orderPutDetailedParam.getGoodsNo();
    	//解决参数中的 英文括号
    	if (goodsNo!=null&&goodsNo!="") {
    		goodsNo = goodsNo.replace("& #40;", "(");
    		goodsNo = goodsNo.replace("& #41;", ")");
    		orderPutDetailedParam.setGoodsNo(goodsNo);
    	}
        this.orderPutDetailedService.update(orderPutDetailedParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(OrderPutDetailedParam orderPutDetailedParam) {
        this.orderPutDetailedService.delete(orderPutDetailedParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(OrderPutDetailedParam orderPutDetailedParam) {
        OrderPutDetailed detail = this.orderPutDetailedService.getById(orderPutDetailedParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(OrderPutDetailedParam orderPutDetailedParam,String condition ) {
        return this.orderPutDetailedService.findPageBySpec(orderPutDetailedParam,condition);
    }

}


